
layui.use(['table', 'layer', 'form'], function() {
	let table = layui.table;
	let add_layer = layui.layer;
	let form = layui.form;

	function pullData() {
		table.render({
			elem: '#dataTbale',
			height: 600,
			url: 'http://chonghekj.com/crud/index.php/Home/Index/getItemList',
			request: {
				pageName: 'pageNumber',
				limitName: 'pageSize'
			},
			page: true,
			where: {
				item_name: $('#demoReload').val(),
				item_state: $('#kucun').val()
			},
			cols: [
				[{
						type: 'checkbox'
					},
					{
						field: 'item_id',
						title: 'ID',
						width: 80
					},
					{
						field: 'item_name',
						title: '物品名称',
						width: 100
					},
					{
						field: 'item_price',
						title: '单价',
						width: 80
					},
					{
						field: 'item_format',
						title: '单位',
						width: 80
					},
					{
						field: 'item_total',
						title: '数量',
						width: 80
					},
					{
						field: 'item_state',
						title: '库存状态',
						width: 150
					},
					{
						field: 'item_inventory',
						title: '库存预测',
						width: 150
					},
					{
						field: 'create_time',
						title: '创建时间',
						width: 200
					},
					{
						field: 'update_time',
						title: '修改时间',
						width: 200
					},
					{
						fixed: 'right',
						title: '操作',
						toolbar: '#barDemo',
						width: 170,
						unresize: true,
						sort: true,
					}
				]
			],
			parseData: function(res) {
				return {
					"code": 0,
					"msg": '',
					"count": res.total,
					"data": res.rows
				}
			},
		})
	}
	pullData();
	$('.add_btn').click(function() {
		add_layer.open({
			type: 1,
			content: $('.add_form'),
			title: '添加物品',
			offset: 'auto',
			// btn: ['取消', '提交'],
			area: ['600px', '600px'],
			shade: [0.8, '#393D49'],
			anim: 5,
			// btnAlign: 'c',
		})
		$('#submit_btn').click(function() {
			form.on('submit(formDemo)', function(data) {
				let item_name = $('.add_item_name').val();
				let item_price = $('.add_item_price').val();
				let item_format = $('.add_item_company').val();
				let item_total = $('.add_item_number').val();
				let item_inventory = $('.add_item_stock').val();
				$.ajax({
					url: 'http://chonghekj.com/crud/index.php/Home/Index/addItem',
					type: 'post',
					dataType: 'json',
					data: {
						item_name,
						item_price,
						item_format,
						item_total,
						item_inventory
					},
					success: function(res) {
						console.log(res);
						pullData();
						add_layer.closeAll();
					}
				})
			});
		})
		$('#cancel_btn').click(function() {
			add_layer.closeAll();
		})
	})
	let array = [];
	let ids = [];
	table.on('checkbox(dataTbale)', function(obj) {
		if (obj.checked) {
			array.push(obj.data);
		} else {
			array.pop(obj.data)
		}
		console.log(array);
		$('.delete_btn').click(function() {
			for (let i = 0; i < array.length; i++) {
				ids.push(array[i].item_id);
				// console.log(ids);
			}
			$.ajax({
				url: 'http://chonghekj.com/crud/index.php/Home/Index/deleteItem',
				type: 'post',
				dataType: 'json',
				data: {
					ids
				},
				success: function(res) {
					// console.log(res);
					pullData();
				}
			})
		})
		// if(array.length == 1){
		// console.log(array);
		$('.modify_btn').click(function() {
			if (array.length == 1) {
				let item_name = array[0].item_name;
				let item_price = array[0].item_price;
				// console.log(item_name);
				let item_id = array[0].item_id;
				let item_format = array[0].item_format;
				let item_total = array[0].item_total;
				let item_inventory = array[0].item_inventory;
				$('.modify_item_name').val(item_name);
				$('.modify_item_price').val(item_price);
				$('.modify_item_company').val(item_format);
				$('.modify_item_number').val(item_total);
				$('.modify_item_stock').val(item_inventory);
				add_layer.open({
					type: 1,
					content: $('.modify_form'),
					title: '修改物品',
					offset: 'auto',
					btn: ['取消', '提交'],
					area: ['600px', '600px'],
					shade: [0.8, '#393D49'],
					anim: 5,
					btnAlign: 'c',
					btn2: function() {
						item_name = $('.modify_item_name').val();
						item_price = $('.modify_item_price').val();
						item_format = $('.modify_item_company').val();
						item_total = $('.modify_item_number').val();
						item_inventory = $('.modify_item_stock').val();
						$.ajax({
							url: 'http://chonghekj.com/crud/index.php/Home/Index/editItem',
							type: 'post',
							dataType: 'json',
							data: {
								item_name,
								item_id,
								item_format,
								item_total,
								item_inventory
							},
							success: function(res) {
								pullData();
							}
						})
					}
				})
			}
		})
		// }
	})
	$('#search').click(function() {
		// query_item_name = $('#demoReload').val();
		pullData();
		// console.log($('#kucun').val())
	})
	table.on('tool(dataTbale)', function(obj) {
		console.log(obj);
		if (obj.event == 'see') {
			// console.log(obj.data)
			add_layer.open({
				type: 1,
				content: $('.viewData'),
				title: '物品详细',
				offset: 'auto',
				// area: ['600px','600px'],
				shade: [0.8, '#393D49'],
				anim: 5,
			})
			$('.name').val(obj.data.item_name);
			$('.unitPrice').val(obj.data.item_price);
			$('.company').val(obj.data.item_format);
			$('.number').val(obj.data.item_total);
			$('.stork').val(obj.data.item_state);
		}
		if (obj.event == 'edit') {
			let item_name = obj.data.item_name;
			let item_price = obj.data.item_price;
			// console.log(item_name);
			let item_id = obj.data.item_id;
			let item_format = obj.data.item_format;
			let item_total = obj.data.item_total;
			let item_inventory = obj.data.item_inventory;
			$('.modify2_item_name').val(item_name);
			$('.modify2_item_price').val(item_price);
			$('.modify2_item_company').val(item_format);
			$('.modify2_item_number').val(item_total);
			$('.modify2_item_stock').val(item_inventory);
			add_layer.open({
				type: 1,
				content: $('.modify2_form'),
				title: '修改物品',
				offset: 'auto',
				btn: ['取消', '提交'],
				area: ['600px', '600px'],
				shade: [0.8, '#393D49'],
				anim: 5,
				btnAlign: 'c',
				btn2: function() {
					item_name = $('.modify2_item_name').val();
					item_price = $('.modify2_item_price').val();
					item_format = $('.modify2_item_company').val();
					item_total = $('.modify2_item_number').val();
					item_inventory = $('.modify2_item_stock').val();
					$.ajax({
						url: 'http://chonghekj.com/crud/index.php/Home/Index/editItem',
						type: 'post',
						dataType: 'json',
						data: {
							item_name,
							item_id,
							item_format,
							item_total,
							item_inventory
						},
						success: function(res) {
							pullData();
						}
					})
				}
			})
		}
		let ids = [];
		if (obj.event == 'del') {
			ids.push(obj.data.item_id);
			$.ajax({
				url: 'http://chonghekj.com/crud/index.php/Home/Index/deleteItem',
				type: 'post',
				dataType: 'json',
				data: {
					ids
				},
				success: function(res) {
					// console.log(res);
					pullData();
				}
			})
		}
	})
});